The following example demonstrates how to filter the data items displayed in a grid using the Filter event. Only the data items whose ShipVia property value is "3" will be displayed in the grid.
XAML |
Copy Code |
---|---|
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"> <Grid.Resources> <xcdg:DataGridCollectionViewSource x:Key="cvs_orders" Source="{Binding Source={x:Static Application.Current}, Path=Orders}" Filter="ShipViaFilter"/> </Grid.Resources> <xcdg:DataGridControl x:Name="OrdersGrid" ItemsSource="{Binding Source={StaticResource cvs_orders}}"/> </Grid> |
The following code provides the implementation of the ShipViaFilter event. This code should be placed in the "code-behind" of your XAML page.
VB.NET |
Copy Code |
---|---|
Private Sub ShipViaFilter( sender As Object, e As FilterEventArgs ) Dim value As Object = CType( e.Item, System.Data.DataRow )( "ShipVia" ) If( Not value Is Nothing ) And ( Not value Is DBNull.Value ) Then If CInt( value ) = 3 Then e.Accepted = True Else e.Accepted = False End If End If End Sub |
C# |
Copy Code |
---|---|
private void ShipViaFilter( object sender, FilterEventArgs e ) { object value = ( ( System.Data.DataRow )e.Item )[ "ShipVia" ]; if( ( value != null ) && ( value != DBNull.Value ) ) { if( ( int )value == 3 ) { e.Accepted = true; } else { e.Accepted = false; } } } |
The next example demonstrates how to filter data items using the Filter predicate delegate.
VB.NET |
Copy Code |
---|---|
Dim view As New DataGridCollectionView( Orders ) view.Filter = New Predicate(Of Object)( ShipViaFilter ) dataGridControl.ItemsSource = view Private Function ShipViaFilter( item As Object ) As Boolean Dim value As Object = TryCast( item, System.Data.DataRow )( "ShipVia" ) If( Not value Is Nothing ) And ( value <> DBNull.Value )Then If CInt( value ) == 3 Then Return True End If End If Return false End Function |
C# |
Copy Code |
---|---|
DataGridCollectionView view = new DataGridCollectionView( Orders ); view.Filter = new Predicate<object>( ShipViaFilter ); dataGridControl.ItemsSource = view; private bool ShipViaFilter( object item ) { object value = ( ( System.Data.DataRow )item )[ "ShipVia" ]; if( ( value != null ) && ( value != DBNull.Value ) ) { if( ( int )value == 3 ) return true; } return false; } |